<?php

namespace Admin\Controller;
use Think\Controller;

class NoticeController extends BaseController{
	
	public function index(){		
		$model = M('notice');
		$count = $model->count();
		$page = new \Think\Page($count,10);
		$show = $page->show();
		$list = $model->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
		foreach($list as $k=>$v){
			$list[$k]['lang'] = $this->getLangName($v['lang']);
		}
		
		$this->assign('show',$show);
		$this->assign('list',$list);
		$this->display();
	}
	
	// 添加
	public function add(){
		if(IS_POST){			
			$data = I('post.');

			$data['addtime'] = time();
			$res = M('notice')->add($data);
			if(!$res) $this->error('fail!');

			$this->success('success!', U('Admin/Notice/index'));
		}

		$this->assign('langs', $this->getLangs($info['lang']));
		$this->display();
	}
	
	// 删除
	public function delete(){
		$id = I('id');
		
		$res = M('notice')->where("id = $id")->delete();
		if(!$res) $this->error('fail！');
		
		$this->success('success！');
	}

	public function getLangName($key){
		$langs = $this->getLangs();
		foreach($langs as $vo){
			if($vo['k']==$key) return $vo['v'];
		}

		return '--';
	}

	public function getLangs($key=''){
		$langs = [
			["k"=>"en","v"=>"English"],
	     	["k"=>"zh-TW","v"=>"繁體中文"],
	  		["k"=>"ma-ma","v"=>"Melayu"],
	  		["k"=>"vi-vn","v"=>"Tiếng Việt"]
		];
		if($key!=''){
			foreach ($langs as $k=>$vo) {
				if($vo['k']==$key){
					$langs[$k]['checked']='selected';
 				}else{
 					$langs[$k]['checked']='';
 				}
			}
		}
		return $langs;
	}

	// 修改
	public function edit(){
		if(IS_POST){			
			$data = I('post.');

			$res = M('notice')->save($data);
			if(!$res) $this->error('fail!');

			$this->success('success!', U('Admin/Notice/index'));
		}

		$id = I('id');
		$info = M('notice')->where("id=$id")->find();

		$this->assign('langs', $this->getLangs($info['lang']));
		$this->assign('info',$info);
		$this->display();
	}

	// 设置状态
	public function setStatus(){
		$data = I('get.');

		$res = M('notice')->save($data);
		if(!$res) $this->error('fail!');

		$this->success('success!');
	}
}
?>